package woa.sort.test20241118;

import java.util.Arrays;

/**
 * @author wangpeng
 * @version 1.0
 * @date 2024/11/18 19:44
 */
public class BubbleSort {

    public static void main(String[] args) {
        int[] arr = {5, 3, 2, 4, 1};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr) {
        int n = arr.length;
        if (n <= 1) {
            return;
        }
        for (int i = 1; i < n; i++) {
            boolean sawp = false;
            for (int j = 0; j < n - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    sawp = true;
                }
            }
            if (!sawp) {
                break;
            }
        }
    }
}
